package com.caigou.modules.rank.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.rank.bean.*
import com.caigou.modules.rank.repository.RankRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/11/01
 */
@HiltViewModel
class RankViewModel @Inject constructor(private val repository: RankRepository) : ViewModel() {

    val fansRankViewModel = MutableLiveData<Resource<FansRankBean>>()
    val levelRankViewModel = MutableLiveData<Resource<LevelRankBean>>()
    val charmRankViewModel = MutableLiveData<Resource<CharmRankBean>>()
    val contributionRankViewModel = MutableLiveData<Resource<ContributionRankBean>>()
    val masterRankViewModel = MutableLiveData<Resource<MasterRankBean>>()

    fun getFansRank(date: String, userId: String) {
        fansRankViewModel.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFansRank(date, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FansRankBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    fansRankViewModel.value = it
                }
        }
    }

    fun getLevelRank(date: String, userId: String) {
        levelRankViewModel.value = Resource.Loading()
        viewModelScope.launch {
            repository.getLevelRank(date, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<LevelRankBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    levelRankViewModel.value = it
                }
        }
    }

    fun getCharmRank(date: String, userId: String) {
        charmRankViewModel.value = Resource.Loading()
        viewModelScope.launch {
            repository.getCharmRank(date, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<CharmRankBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    charmRankViewModel.value = it
                }
        }
    }

    fun getContributionRank(date: String, userId: String) {
        contributionRankViewModel.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContributionRank(date, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<ContributionRankBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    contributionRankViewModel.value = it
                }
        }
    }

    fun getMasterRank(date: String, userId: String) {
        masterRankViewModel.value = Resource.Loading()
        viewModelScope.launch {
            repository.getMasterRank(date, userId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<MasterRankBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    masterRankViewModel.value = it
                }
        }
    }
}